//
//  SCRNManager+Contract.m
//  Shuaiche
//
//  Created by xiangtao on 2025/2/7.
//  Copyright © 2025 ShuaiChe. All rights reserved.
//

#import "SCRNManager+Contract.h"
#import "SCDownloadManager.h"
#import <WXApi.h>

@implementation SCRNManager (Contract)

//车辆详情
RCT_EXPORT_METHOD(downloadContract:(NSDictionary *)params)
{
    dispatch_async(dispatch_get_main_queue(), ^(){
        NSString *destination = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        NSString *url = [params objectForKey:@"downloadUrl" defaultValue:nil];
        NSString *fileName = [params objectForKey:@"fileName" defaultValue:nil];
        [[SCDownloadManager sharedInstance] downloadTaskWithUrl:url version:nil progress:nil destination:destination completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            NSString *suggestedFilename = response.suggestedFilename;
            if (suggestedFilename && !error) {
                
                //阻止document目录的db被同步到icloud
                NSError *error1 = nil;
                BOOL success = [filePath setResourceValue:[NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error1];
                if(!success){
                    LogInfo(@"Error excluding %@ from backup %@", [filePath lastPathComponent], error1);
                }
                
                LogInfo(@"fileName = %@",fileName);
                
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = fileName;
                message.description = @"电子合同";
                [message setThumbImage:[UIImage imageNamed:@"report_pdf"]];
                
                WXFileObject *ext = [WXFileObject object];
                ext.fileExtension = @"pdf";
                ext.fileData = [NSData dataWithContentsOfFile:filePath.path];
                
                message.mediaObject = ext;
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = WXSceneSession;
                
                [WXApi sendReq:req completion:nil];
                
            }
        }];
    });
}

@end
